package com.bawei.module_base.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;


public class FlowLayout extends ViewGroup {
    public FlowLayout(Context context) {
        super(context);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
    }

    //        5.自定义ViewGroup，自动换行展示搜索历史内容  17:34
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int top=10;
        int left=10;
        for (int i = 0; i < getChildCount(); i++) {
            View childAt = getChildAt(i);
            int measuredHeight = childAt.getMeasuredHeight();
            int measuredWidth = childAt.getMeasuredWidth();
            if (left+measuredWidth>getWidth()){
                left=10;
                top+=measuredHeight;
            }
            childAt.layout(left,top,left+measuredWidth,top+measuredHeight);
            left+=measuredWidth;
        }
    }

}
